extends State
class_name PlayerStateMove

# 重构后的玩家移动状态 - 简化逻辑，分离关注点

const MIN_JUMP_TIME: float = 0.1  # 最小跳跃时间

var coyote_duration: float = 0.2  # 使用秒而非毫秒
var coyote_timer: float = 0.0
var is_falling: bool = false
var is_move:bool = false
var input_handler: InputHandler
var jump_press_time: float = 0

func enter(_data: Dictionary = {}) -> void:
	if Global.current_setting.get("MODE",2) <= 1:
		coyote_duration = 0.3
	if character.velocity.is_equal_approx(Vector2.ZERO):
		animation.play("idle")
		is_move = false
		MusicPlayer.stop_sfx("Move")
	else:
		animation.play("move")
		is_move = true
		MusicPlayer.play_sfx("Move")
	character.can_jump = false
	if character.current_dash_count < 1:
		character.restore_dash(1)
	# 获取输入处理器
	input_handler = character.get_node("InputHandler") as InputHandler
	if input_handler:
		input_handler.movement_input.connect(_on_movement_input)
		input_handler.jump_pressed.connect(_on_jump_pressed)

func exit() -> void:
	# 断开信号连接
	MusicPlayer.stop_sfx("Move")
	if input_handler:
		if input_handler.movement_input.is_connected(_on_movement_input):
			input_handler.movement_input.disconnect(_on_movement_input)
		if input_handler.jump_pressed.is_connected(_on_jump_pressed):
			input_handler.jump_pressed.disconnect(_on_jump_pressed)

	jump_press_time = 0

func update(delta: float) -> void:
	_update_coyote_time(delta)
	_check_fall_transition()
	_update_animation()


func physics_update(_delta: float) -> void:
	# 物理更新在这里处理，但移动逻辑由输入处理器驱动
	pass

# 处理移动输入
func _on_movement_input(direction: Vector2) -> void:
	if not character.can_move:
		return
	
	var target_velocity = direction.x * character.speed
	character.velocity.x = move_toward(
		character.velocity.x, 
		target_velocity, 
		character.acceleation * get_physics_process_delta_time()
	)




# 处理跳跃按下
func _on_jump_pressed() -> void:
	character.can_jump = false
	character.velocity.y = -character.jump_speed
	input_handler.consume_buffered_input("jump")
	MusicPlayer.play_sfx("Jump")
	change_state("jump")
	




# 更新土狼时间
func _update_coyote_time(delta: float) -> void:
	if character.is_on_floor():
		coyote_timer = coyote_duration
		is_falling = false
	else:
		coyote_timer = max(0.0, coyote_timer - delta)
		if character.velocity.y > 0:
			is_falling = true

# 检查是否应该转换到下落状态
func _check_fall_transition() -> void:
	if is_falling and coyote_timer <= 0.0:
		character.can_jump = false
		change_state("fall")

# 检查是否可以跳跃
func _can_jump() -> bool:
	return (coyote_timer > 0.0 && input_handler.has_buffered_input("jump")) && character.can_jump

func can_transition_to(_target_state: String) -> bool:
	return true



# 更新动画
func _update_animation() -> void:
	if not animation:
		return
	
	if character.velocity.is_equal_approx(Vector2.ZERO) && is_move:
		animation.play("idle")
		is_move = false
		MusicPlayer.stop_sfx("Move")
	elif is_move == false && !character.velocity.is_equal_approx(Vector2.ZERO):
		is_move = true
		animation.play("move")
		MusicPlayer.play_sfx("Move")

# 获取状态数据
func get_state_data() -> Dictionary:
	return {
		"coyote_timer": coyote_timer,
		"is_falling": is_falling,
		"can_jump": _can_jump()
	}

## 调试信息
#func get_debug_info() -> String:
	#return "Move State - Coyote: %.2f, Falling: %s, Velocity: %s" % [
		#coyote_timer, 
		#is_falling, 
		#character.velocity
	#]
